//
//  GYTabBar.swift
//  Weibo
//
//  Created by vane on 16/8/9.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

protocol GYTabBarDelegate:NSObjectProtocol{

    func tabBarButtonClick()
}

class GYTabBar: UITabBar {

    weak var gyDelegate:GYTabBarDelegate?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private lazy var btn:UIButton = {
        
        let button = UIButton()

        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)

        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        // 自适应
        button.sizeToFit()
        return button
        
    }()
    
    func btnClick(){
        
        gyDelegate?.tabBarButtonClick()
    }
    
    func setupUI(){
        
        addSubview(btn)
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let childW = self.frame.width * 0.2
        var index: CGFloat = 0
        
        for value in self.subviews{
            
            if value .isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                value.frame.size.width = childW
                value.frame.origin.x = index * childW
                
                index++
                if index == 2 {
                    
                    index++
                }
            }
        }
        btn.center.x = self.frame.width * 0.5
        btn.center.y = self.frame.height * 0.5
        
    }

    
    
}
